<?php
declare(strict_types=1);

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Logic\Record as RecordLogic;
use Illuminate\Support\Facades\DB;

class RecordController extends Controller
{
    private $recordLogic;

    public function __construct(RecordLogic $recordLogic){
        $this->recordLogic = $recordLogic;
    }

    public function index(Request $request)
    {
        return view('record.index');
    }

    public function list(Request $request)
    {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 10);
        $start = ($page - 1) * $limit;
        $where['log_menu'] = $request->input('menu');
        $where['log_account'] = $request->input('account');
        $where['log_ip'] = $request->input('ip');
        $where['create_time'] = $request->input('time');
        $result = [
            'code' => 0,
            'msg' => '获取数据成功！',
            'count' => $this->recordLogic->count($where),
            'data' => $this->recordLogic->list((int)$start, (int)$limit, $where),
        ];
        return response()->json($result);
    }

    public function delete(int $id)
    {
        $result = ['status' => false, 'msg' => '数据删除失败！',];
        if ($id > 0){
            if ($this->recordLogic->delete((int)$id)) {
                $result['status'] = true;
                $result['msg'] = '数据删除成功！';
                return response()->json($result);
            }
        }
        return response()->json($result);
    }

    public function remove(Request $request)
    {
        $result = ['status' => false, 'msg' => '批量删除数据失败！',];
        $ids = $request->input('ids');
        if (!empty($ids)) {
            $ids = explode(',', $ids);
            if ($this->recordLogic->remove($ids)) {
                $result['status'] = true;
                $result['msg'] = '批量删除数据成功！';
                return response()->json($result);
            }
        }
        return response()->json($result);
    }

    public function clear()
    {
        $result = ['status' => false, 'msg' => '清空日志失败！',];
        if (DB::table("system_logs")->delete()){
            $result['status'] = true;
            $result['msg'] = "清空日志成功！";
            return response()->json($result);
        }
        return response()->json($result);
    }
}
